#include "t0022-0.h"
#include "dxtviewer.h"
#include "appwidget.h"
#include "cui.h"
#include <signal.h>

#define MODNAME     "[POPM]"
extern struct dxtviewer_st dv;

#define POPFORM_FIELD_POINTNO               0
#define POPFORM_FIELD_POINTVALUE         1

int popform_keyevent(struct cui_widget_st* widget, int key)
{
    int handled = CUI_WIDGETEVENT_PROPAGATION;
    ERRSYS_INFOPRINT("!!!!!! %s:%s %s(%d) \n", widget->name, __func__, cui_key_str(key), key);
    switch (key) {
		case KEY_ENTER_US:
			key = KEY_ENTER;
		case KEY_ENTER:
		case KEY_ESC:
			handled = CUI_WIDGETEVENT_HANDLED;
			break;
	}
    if (key == KEY_ENTER) {
        struct appdata_st *appdata = widget->priv;
        if (appdata) {
            struct l0032_xtable_entry *entry = appdata->entry;
            struct cui_form_st *cform = widget->widgetinstance;
            char *buf = field_buffer(cform->fields[POPFORM_FIELD_POINTVALUE], 0);
            int value = 0;
            cui_trim_buf(buf);
            ERRSYS_INFOPRINT("%s buf = [%s]\n", __func__, buf);
            if (strlen(buf) != 0) {
                if (is_integer(buf)){
                    value = strtol(buf, NULL, 10);
                    if (errno == 0) {
                        ERRSYS_INFOPRINT("%s xtable wrote pt=%d val=%d\n", __func__, appdata->point_index, value);
                        l0032_xtable_write(entry->hentry, appdata->point_index, &value);
                        key = KEY_ESC;  // written and quit
                    }
                }
            }
        }
    }
    
    if (key == KEY_ESC) {
        cui_screen_pop(widget->cscr);
        cui_refresh(widget->cscr, CUI_CURSOR_INVISIBLE);
        ERRSYS_INFOPRINT("popform quit\n");
    }
    return handled;
}

int popform_load(struct appdata_st *appdata)
{
    int retval = -1;
    struct cui_widget_st *widget = get_widget_byname(&dv.cscr->windowlist, "popform");
    struct cui_field_st cfield; // cfield template
    const char *item_namelist[2] = 
    {
        "Point    : ",
         "New Value: ",
    };
    char title[CUI_WIDGETTITLE_BUFLEN] = {0};
    char value[16] = {0};
    struct l0032_xtable_entry *entry = appdata->entry;
    int val;

    ERRSYS_INFOPRINT("%s widget %p\n", __func__, widget);
    cui_dump_widget_hierarchy(widget);
    if (widget) {
        int n = sizeof(item_namelist) / sizeof(item_namelist[0]);
        widget->priv = appdata;

        snprintf(title, CUI_WIDGETTITLE_BUFLEN, "Data Settings");
        cui_set_widget_title(widget, title);
        // initialize cfield template
        cui_initialize_dummy_field(&cfield);
        cui_form_initialize(widget, n, item_namelist, &cfield);
        cui_widget_align(widget, CUI_WIDGET_ALIGNMENT_XYMID);

        snprintf(value, 16, "%d", appdata->point_index);
        cui_field_setvalue(widget, POPFORM_FIELD_POINTNO, value);
        cui_field_setoptions(widget, POPFORM_FIELD_POINTNO, O_ACTIVE | O_EDIT, FALSE);
        cui_field_setfocus(widget, POPFORM_FIELD_POINTVALUE);
        
        l0032_xtable_read(entry->hentry, appdata->point_index, &val, sizeof(int));
        snprintf(value, 16, "%d", val);
        cui_field_setvalue(widget, POPFORM_FIELD_POINTVALUE, value);

        cui_set_widget_eventslot(widget, CUI_EVENTCB_TYPE_KEYEVENT, popform_keyevent);
        cui_screen_push(widget, NULL);
        cui_refresh(widget->cscr, CUI_CURSOR_VERYVISIBLE);
    }

    return retval;
}
